package com.itheima.sc.impl.ums;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.sc.api.ums.AdminApi;
import com.itheima.sc.mapper.ums.AdminMapper;
import com.itheima.sc.pojo.ums.Admin;
import com.itheima.sc.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class AdminImpl implements AdminApi {

    @Autowired
    private AdminMapper adminMapper;

    //查询用户数据
    public Admin selectUser(String username) {
        //构建查询条件
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(username), Admin::getUsername, username);
        return adminMapper.selectOne(wrapper);
    }

    @Override
    public Admin selectAdminById(Long adminId) {
        return adminMapper.selectById(adminId);
    }

    /**
     * 用户名或姓名分页获取用户列表
     */
    @Override
    public PageResult selectPage(Integer pageNum, Integer pageSize, String keyword) {
        Page<Admin> page = new Page<>(pageNum, pageSize);
        //构建查询条件
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        //TODO 双条件模糊查询
        wrapper.like(StrUtil.isNotBlank(keyword), Admin::getUsername, keyword).or().like(StrUtil.isNotBlank(keyword), Admin::getNickName, keyword);
        wrapper.orderByDesc(Admin::getCreateTime);
        adminMapper.selectPage(page, wrapper);

        //封装分页数据
        PageResult pageResult = new PageResult();
        pageResult.setPageSize(pageSize);
        pageResult.setPageNum(pageNum);
        pageResult.setTotal(Convert.toInt(page.getTotal()));
        pageResult.setTotalPage(Convert.toInt(page.getPages()));
        pageResult.setList(page.getRecords());


        //返回结果
        return pageResult;
    }

    /**
     * 添加用户
     */
    @Override
    public void register(Admin admin) {
        adminMapper.insert(admin);
    }

    /**
     * 修改指定用户信息
     */
    @Override
    public void update(Admin admin) {
        adminMapper.updateById(admin);
    }

    /**
     * 根据用户名和密码判断用户是否存在
     */
    @Override
    public Admin isExit(String username, String oldPassword) {
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(username), Admin::getUsername, username);
        wrapper.eq(StrUtil.isNotBlank(oldPassword), Admin::getPassword, oldPassword);
        return adminMapper.selectOne(wrapper);
    }

    /**
     * 修改账号状态
     */
    @Override
    public void updateStatus(Integer id, Integer status) {
        adminMapper.updateStatus(id,status);
    }

    @Override
    public void delete(Integer id) {
        adminMapper.deleteById(id);
    }

    /**
     *根据当前用户的id,获取用户信息
     */
    @Override
    public Admin getAdminById(Long adminId) {
        return adminMapper.selectById(adminId);
    }


}
